package keyboard.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JComponent;

import keyboard.model.Keyboard;

import main.model.Settings;

@SuppressWarnings("serial")
abstract class AbstractKey extends JComponent implements MouseListener, Observer {
	
	private Settings settings;
	private String key;
	private String capsKey;
	protected String active;
	protected Keyboard keyboard;
	
	public AbstractKey(Keyboard keyboard, String key, String capsKey) {
		settings = new Settings();
		this.keyboard = keyboard;
		this.key = key;
		this.capsKey = capsKey;
		this.active = key;
		
		addMouseListener(this);
	}
	
	protected abstract void click();
	
	
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		
		g.drawRect(0, 0, getBounds().width-1, getBounds().height-1);
		g.drawString(active, 20, 20);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (settings.clickToClick())
			click();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (settings.enterToClick())
			click();
	}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {}

	@Override
	public void mouseReleased(MouseEvent e) {}

	@Override
	public void update(Observable arg0, Object arg1) {
		if(keyboard.isCaps())
			active = capsKey;
		else
			active = key;
		repaint();
	}
}